<template>
	<view class="L-main">
		<view class="search">
			<view class="inp">
				<input placeholder="请输入政策标题" v-model="keyWord" @confirm="search"/>
				<view class="btn" @click="search">
					<image src="../../static/workMessage/search.png"></image>
					<text>搜索</text>
				</view>
			</view>
		</view>
		<view class="nav">
			<view class="navList">
				<view :class="[navIndex === index?'active':'','navBox']" @click="navClick(index,item.code)" v-for="(item,index) in navList" :key="index">
					{{ item.name }}
				</view>
			</view>
		</view>
		<view class="news_list">
			<view class="title">
				<text>政策清单</text>
				<text @click="searchAll">全部</text>
			</view>
			<view class="scroll">
				<scroll-view scroll-y style="height: 100%;" @scrolltolower="lower">
					<view class="list">
						<view class="list_box" v-for="item in list" :key="item.id" @click="toDetail(item.id)">
							<view>
								<text>{{ item.title }}</text>
								<image src="../../static/workMessage/right.png"></image>
							</view>
							<view>{{ item.pubTime }}</view>
						</view>
						<uni-load-more :status="status" v-if="list.length != 0"></uni-load-more>
						<view class="noData" v-if="list.length == 0 && status == 'nomore'">
							<image src="../../static/img_nodata.png"></image>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	export default{
		data(){
			return{
				type:'',
				keyWord:'',
				list:[],
				navList:[],
				navIndex:'',
				start:0,
				length:10,
				status:'more'
			}
		},
		onLoad(){
			this.selectByTypeList();
			this.selectDic();
		},
		watch:{
			keyWord(newVal,oldVal){
				if((/^\s*$/.test(newVal))){
					this.list = [];
					this.start = 0;
					this.status = 'more';
					this.selectByTypeList();
				}
			}
		},
		methods:{
			toDetail(id){
				uni.navigateTo({
					url:'/pages/bright/unscramble?id=' + id
				})
			},
			searchAll(){
				this.keyWord = '';
				this.reset();
				this.selectByTypeList();
			},
			reset(){
				this.list = [];
				this.navIndex = '';
				this.type = '';
				this.start = 0;
				this.status = 'more';
			},
			lower(){
				if(this.status != 'more'){
					return false
				}
				this.selectByTypeList();
			},
			selectDic(){
				this.$ajax({
					url: '/api/news/zwNewsPolicy/selectDic',
					needAuth: true,
				}).then(res => {
					if (res.code == 200) {
						this.navList = res.data;
					}
				})
			},
			search(){
				if (/^\s*$/.test(this.keyWord)) {
					uni.showToast({
						title: '请输入关键字',
						icon: 'none'
					})
				}
				this.list = [];
				this.start = 0;
				this.status = 'more';
				this.selectByTypeList();
			},
			navClick(index,code){
				if(index !== this.navIndex){
					this.reset();
					this.type = code;
					this.navIndex = index;
					this.selectByTypeList();
				}
			},
			selectByTypeList() {
				uni.showLoading({
					title: '搜索中...',
					mask: true
				});
				this.status = 'loading'
				this.$ajax({
					url: '/api/news/zwNewsPolicy/selectByTypeList',
					needAuth: true,
					data: {
						type: this.type,
						title:this.keyWord,
						start:this.start,
						length:this.length
					}
				}).then(res => {
					if (res.code == 200) {
						let data = res.data;
						console.log(data)
						this.status = 'more'
						if(data.current >= data.pages || data.records.length == 0){
							this.status = 'nomore'
						}
						this.start = this.start + this.length * data.current;
						console.log(this.start)
						this.list = this.list.concat(data.records);
					}
				}).finally(()=>{
					uni.hideLoading();
				})
			},
		}
	}
</script>

<style lang="less">
	.L-main {
		display: flex;
		flex-direction: column;
		position: absolute;
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		height: 100%;
		.noData {
			display: flex;
			justify-content: center;
			align-items: center;
			padding-top: 30rpx;
		
			image {
				width: 408rpx;
				height: 319rpx;
			}
		}
		.search {
			display: flex;
			justify-content: center;
			padding-top: 25rpx;

			.inp {
				position: relative;
				width: 584rpx;
				border-radius: 28rpx;
				border: solid 2rpx #328ef9;
				box-sizing: border-box;
				height: 64rpx;

				input {
					height: 100%;
					width: 400rpx;
					font-size: 26rpx;
					padding-left: 60rpx;
					box-sizing: border-box;
				}

				.btn {
					width: 103rpx;
					height: 56rpx;
					background-color: #328ef9;
					border-radius: 26rpx;
					border: solid 2rpx #328ef9;
					display: flex;
					align-items: center;
					justify-content: space-between;
					padding: 0 20rpx;
					position: absolute;
					right: 0;
					top: 0;

					image {
						width: 30rpx;
						height: 30rpx;
					}

					text {
						font-size: 26rpx;
						line-height: 26rpx;
						color: #FFFFFF;
					}
				}
			}
		}

		.nav {
			padding: 0 30rpx;
			margin-top: 40rpx;
			
			.navList{
				display: flex;
				justify-content: space-between;
				.navBox {
					width: 161rpx;
					height: 97rpx;
					background-image: linear-gradient(-90deg,
							#94b9ff 0%,
							#4191fd 100%),
						linear-gradient(#477fe9,
							#477fe9);
					background-blend-mode: normal,
						normal;
					border-radius: 4rpx;
					color: #ffffff;
					font-size: 28rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					filter: grayscale(1);
				}
				.active{
					filter: grayscale(0);
				}
			}
		}

		.news_list {
			margin-top: 20rpx;
			flex: 1;
			overflow: hidden;
			display: flex;
			flex-direction: column;

			.title {
				padding: 0 30rpx;
				height: 100rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				text {
					&:nth-child(1) {
						color: #333333;
						font-size: 34rpx;
						font-weight: bold;
					}

					&:nth-child(2) {
						color: #999999;
						font-size: 28rpx;
						display: flex;
						align-items: center;
						height: 100%;
					}
				}
			}

			.scroll {
				flex: 1;
				overflow: hidden;
				.list {
					padding: 0 30rpx;
					.list_box {
						border-bottom: 1rpx solid #f1f1f1;
						padding-bottom: 20rpx;
						margin-bottom: 50rpx;

						view {
							&:nth-child(1) {
								display: flex;
								justify-content: space-between;
								align-items: center;
								margin-bottom: 20rpx;

								text {
									font-size: 28rpx;
									color: #333333;
									display: inline-block;
									width: 570rpx;
									overflow: hidden;
									display: -webkit-box;
									-webkit-line-clamp: 2;
									-webkit-box-orient: vertical;

								}

								image {
									width: 22rpx;
									height: 22rpx;
								}
							}

							&:nth-child(2) {
								color: #999999;
								font-size: 24rpx;
								text-align: right;
								padding-right: 25rpx;
							}
						}
						
						&:nth-last-child(2){
							margin-bottom: 0;
						}
					}
				}
			}


		}
	}
</style>
